//
//  AddPlayerViewController.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/6.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit

class AddPlayerViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    var teamModel:TeamModel? {
        didSet{
            teamPlayerArr = TeamModel.getTeamPlayerListWihtTeamID(teamID: self.teamModel!.teamID!)
            for model in teamPlayerArr {
                if model.playerType == "0" {
                    shoufaArr.append(model)
                }else{
                    tibuArr.append(model)
                }
            }
        }
    }
    
    var shoufaArr = [PlayerModel]()
    var tibuArr = [PlayerModel]()
    var teamType:Int!
    var teamPlayerNum:Int = 0
    fileprivate var starterNum = 11 //首发球员数量
    private var benchNum = 12 //替补队员数量
    var matchModel:MatchModel?
    var teamPlayerArr = [PlayerModel]()
    @IBOutlet weak var tableview: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        tableview.register(UINib.init(nibName: "AddPlayerTableViewCell", bundle: nil), forCellReuseIdentifier: "AddPlayerTableViewCell")
        tableview.delegate = self
        tableview.dataSource = self
        weak var __weakself = self
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "insertPlayerModelSuccess"), object: nil, queue: .main) { (rs) in
            let model = rs.userInfo!["model"] as! PlayerModel
            if model.playerType == "0"{
                __weakself?.shoufaArr.append(model)
            }else{
                __weakself?.tibuArr.append(model)
            }
            
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return starterNum
        }else{
            return benchNum
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("AddPlayerTableViewCell", owner: nil, options: nil)?.first as! AddPlayerTableViewCell
        cell.selectionStyle = .none
        cell.cellIndex.text = "\(indexPath.row)"
        cell.teamModel = teamModel
        cell.matchModel = matchModel
        cell.indexP = indexPath as NSIndexPath
        if indexPath.section == 0 {
            if indexPath.row <= shoufaArr.count - 1 {
                cell.setCell(model: shoufaArr[indexPath.row])
            }
        }else{
            if indexPath.row <= tibuArr.count - 1 {
                cell.setCell(model: tibuArr[indexPath.row])
            }
        }
        return cell
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "首发球员(\(starterNum))"
        }else{
            return "替补球员(\(benchNum))"
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 58.0
    }
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let headerview = view as! UITableViewHeaderFooterView
        headerview.textLabel?.textColor = #colorLiteral(red: 1, green: 0.7058823529, blue: 0.2549019608, alpha: 1)
        headerview.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        headerview.backgroundView = UIImageView.init(image: UIImage.creatImage(with: #colorLiteral(red: 0.1725490196, green: 0.1725490196, blue: 0.1725490196, alpha: 1)))
        let imageview = UIImageView.init(image: #imageLiteral(resourceName: "矩形 8"))
        headerview.addSubview(imageview)
        imageview.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(5)
            make.width.equalTo(4)
            make.height.equalTo(12)
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.isEditing = true
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.0001
    }
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
//    // 进入编辑模式，按下出现的编辑按钮后,进行删除操作
//    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//        if editingStyle == .delete{
//            print("删除\(indexPath.section)块\(indexPath.row)行")
//            if indexPath.section == 0 {
//                starterNum -= 1
//            }else{
//                benchNum -= 1
//            }
//            self.tableview.reloadSections(NSIndexSet.init(index: indexPath.section) as IndexSet, with: .fade)
//        }
//    }
}


